package com.pesilo.mvc.view.component.module.homepageModule
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Quad;
	import com.pesilo.base.BaseComponent;
	import com.pesilo.event.PesiloEvent;
	import com.pesilo.mvc.controller.ApplicationFacade;
	import com.pesilo.ui.Starlink;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class HomepageComponent extends BaseComponent
	{
		private var starlink:Starlink;
		private var isStarClick:Boolean=false;
		public function HomepageComponent()
		{
			super();
		}
		
		override public function initUI():void{
			isStarClick=false;
			
			starlink=new Starlink();
			this.addChild(starlink);
			for(var i:uint=0;i<6;i++){
				(starlink.getChildByName("star_"+i) as Sprite).buttonMode=true;
			}
			
			for(var j:uint=0;j<5;j++){
				(starlink.getChildByName("line_"+j) as Sprite).alpha=0;
			}
		}
		
		override public function initEvent(b:Boolean=true):void{
			if(b){
				for(var i:uint=0;i<6;i++){
					(starlink.getChildByName("star_"+i) as Sprite).addEventListener(MouseEvent.ROLL_OVER,__starMouseEventHandle);
					(starlink.getChildByName("star_"+i) as Sprite).addEventListener(MouseEvent.ROLL_OUT,__starMouseEventHandle);
					(starlink.getChildByName("star_"+i) as Sprite).addEventListener(MouseEvent.CLICK,__starMouseEventHandle);
				}
			}else{
				for(var j:uint=0;j<6;j++){
					(starlink.getChildByName("star_"+j) as Sprite).removeEventListener(MouseEvent.ROLL_OVER,__starMouseEventHandle);
					(starlink.getChildByName("star_"+j) as Sprite).removeEventListener(MouseEvent.ROLL_OUT,__starMouseEventHandle);
					(starlink.getChildByName("star_"+j) as Sprite).removeEventListener(MouseEvent.CLICK,__starMouseEventHandle);
				}
			}
		}
		
		protected function __starMouseEventHandle(e:MouseEvent):void
		{
//			var index:int=starlink.getChildIndex(e.target as Sprite);
			var index:int=int(e.target.name.substring(5));
			switch(e.type){
				case MouseEvent.CLICK:
					isStarClick=true;
					
					homepageExit(index);
					
					for(var j:uint=0;j<5;j++){
						TweenMax.to((starlink.getChildByName("line_"+j) as Sprite),0.1,{alpha:0});
					}
					TweenMax.to(e.target,0.5,{glowFilter:{color:0xFFFFFF,blurX:0,blurY:0,ease:Quad.easeIn}});
					break;
				case MouseEvent.ROLL_OVER:
					if(isStarClick)return;
					
					TweenMax.to(e.target,0.3,{glowFilter:{color:0xFFFFFF,blurX:18,blurY:18,ease:Quad.easeOut}});
					for(var k:uint=0;k<index;k++){
						TweenMax.to((starlink.getChildByName("line_"+k) as Sprite),0.5,{delay:0.05*k,alpha:1});
					}
					
					
					break;
				case MouseEvent.ROLL_OUT:
					if(!isStarClick){
						TweenMax.killChildTweensOf(starlink);
					}
					
					for(var l:uint=0;l<5;l++){
						TweenMax.to((starlink.getChildByName("line_"+l) as Sprite),0.5,{alpha:0});
					}
					TweenMax.to(e.target,0.4,{glowFilter:{color:0xFFFFFF,blurX:3,blurY:3,ease:Quad.easeIn}});
					break;
			}
		}
	
		
		private function homepageExit(index:int):void{
			dispatchEvent(new PesiloEvent(PesiloEvent.APP_EXIT_COMPLETE_EVENT,index));
		}
		
		override public function initLocation():void{
			starlink.x=stage.stageWidth/2-starlink.width/2;
			starlink.y=stage.stageHeight/2-starlink.height/2;
		}
		
		public function test():void
		{
			trace("fuck");
		}
	}
}